package com.example;

import java.sql.*;

public class JDBCMain {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        /*// 实例化驱动
        Driver driver = new com.mysql.cj.jdbc.Driver();
        // 注册驱动
        DriverManager.registerDriver(driver);*/
        /* 使用反射加载驱动类 */
        // com.mysql.cj.jdbc.Driver =》驱动版本8以上
        // com.mysql.jdbc.Driver =》驱动是5版本
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url ="jdbc:mysql://localhost:3306/java_web_study";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager
                .getConnection(url, username, password);
        // 相当于执行语句的控制台
        Statement statement = connection.createStatement();
        // 执行sql语句获取结果集
        ResultSet resultSet = statement
                .executeQuery("select * from t_user where id = 1");
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("username");
            String pwd = resultSet.getString("password");
            int sex = resultSet.getInt("sex");
            String description = resultSet.getString("description");
            Date created = resultSet.getDate("created");
            System.out.println(
                    "id:" + id
                    + ", username:" + name
                    + ", password:" + pwd
                    + ", sex:" + sex
                    + ", description:" + description
                    + ", created:" + created
            );
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
